package com.cw.jeeyt.controller.system;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumImportDataEntryType;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.system.SystemDataEntryBo;
import com.cw.jeeyt.service.bo.system.SystemProjectInfoBo;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.jeeyt.service.intf.system.SystemDataEntryService;
import com.cw.jeeyt.service.intf.system.SystemProjectInfoService;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 数据条目信息控制类
 *
 * @author chenw
 * @version 1.0.0
 */
@Controller
@RequestMapping("/systemDataEntry")
public class SystemDataEntryController extends BaseController {
    @Resource
    private SystemDataEntryService systemDataEntryService;
    @Resource
    private SystemProjectInfoService systemProjectInfoService;

    /**
     * 跳转到列表查询页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query")
    public ModelAndView query() throws Exception {
        ModelAndView mv = new ModelAndView("system/systemDataEntry_index");
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 跳转到导入数据条目
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-importFacade")
    public ModelAndView toImport(String selectType, Long objId) throws Exception {
        ModelAndView mv = new ModelAndView("system/systemDataEntry_importIndex");
        mv.addObject("permission", new PermissionDirective());
        mv.addObject("selectType", selectType);
        mv.addObject("objId", objId);
        return mv;
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable 请求数据
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/listSystemDataEntry")
    public ResultData listSystemDataEntry(@RequestBody DataTable dataTable) throws Exception {

        SystemDataEntryBo systemDataEntryBo = JSONObject.parseObject(JSON.toJSONString(dataTable.getQueryCond()),
            SystemDataEntryBo.class);
        if (systemDataEntryBo == null) {
            systemDataEntryBo = new SystemDataEntryBo();
        }
        PageInfo<SystemDataEntryBo> pageInfo = systemDataEntryService.queryByPage(systemDataEntryBo,
            dataTable.getStart(), dataTable.getLength());
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 加载导入接口条目列表数据
     *
     * @param dataTable 请求数据
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/listImportFacadeDataEntry")
    public ResultData listImportFacadeDataEntry(@RequestBody DataTable dataTable, Long objId) throws Exception {

        SystemDataEntryBo systemDataEntryBo = JSONObject.parseObject(JSON.toJSONString(dataTable.getQueryCond()),
            SystemDataEntryBo.class);
        if (systemDataEntryBo == null) {
            systemDataEntryBo = new SystemDataEntryBo();
        }
        PageInfo<SystemDataEntryBo> pageInfo = systemDataEntryService.queryImportFacadeByPage(systemDataEntryBo,
            dataTable.getStart(), dataTable.getLength(), objId);
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable 请求数据
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/listImportSystemDataEntry")
    public ResultData listImportSystemDataEntry(@RequestBody DataTable dataTable, String tableId) throws Exception {

        SystemDataEntryBo systemDataEntryBo = JSONObject.parseObject(JSON.toJSONString(dataTable.getQueryCond()),
            SystemDataEntryBo.class);
        if (systemDataEntryBo == null) {
            systemDataEntryBo = new SystemDataEntryBo();
        }
        PageInfo<SystemDataEntryBo> pageInfo = systemDataEntryService.queryImportByPage(systemDataEntryBo,
            dataTable.getStart(), dataTable.getLength(), tableId);
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 跳转到表单页面
     *
     * @param id 主键
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-form")
    public ModelAndView toForm(String id, String operation) throws Exception {
        ModelAndView mv = new ModelAndView("system/systemDataEntry_form");
        SystemDataEntryBo systemDataEntryBo = null;
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            systemDataEntryBo = new SystemDataEntryBo();
        } else {
            systemDataEntryBo = systemDataEntryService.getSystemDataEntryById(id);
        }
        List<SystemProjectInfoBo> systemProjectInfoList = systemProjectInfoService.querySystemProjectInfoList();
        mv.addObject("systemProjectInfoList", systemProjectInfoList);
        mv.addObject("systemDataEntry", systemDataEntryBo);
        mv.addObject("operation", operation);
        mv.addObject("PageDict", new PageDictDirective());
        return mv;
    }

    /**
     * 根据操作类型保存数据
     *
     * @param response
     * @param systemDataEntryBo
     * @param operation add新增  edit修改 view查看
     */
    @ResponseBody
    @RequestMapping(value = "/saveSystemDataEntry", method = RequestMethod.POST)
    public JsonResultDto saveSystemDataEntry(SystemDataEntryBo systemDataEntryBo, String operation) {
        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                systemDataEntryService.insertSystemDataEntry(systemDataEntryBo);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                systemDataEntryService.updateSystemDataEntry(systemDataEntryBo);
            }
            jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("保存失败:" + e.getMessage());

        }
        jsonResultDto.setOperation(operation);
        return jsonResultDto;
    }

    /**
     * 删除数据条目信息
     *
     * @param systemDataEntryBo
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JsonResultDto deleteSystemDataEntry(SystemDataEntryBo systemDataEntryBo) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            systemDataEntryService.deleteSystemDataEntry(systemDataEntryBo);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();

            jsonResultDto.setMsg("删除失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 校验数据条目信息，如果数据条目编码以及数据条目类型都相同，则返回true，否则返回false
     *
     * @param systemDataEntryBo
     * @throws Exception
     */
    @RequestMapping(value = "/checkDataEntry")
    @ResponseBody
    public JsonResultDto checkSystemDataEntry(SystemDataEntryBo systemDataEntryBo) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            boolean isExist = systemDataEntryService.checkSystemDataEntry(systemDataEntryBo);
            if (isExist) {
                jsonResultDto.setMsg("数据条目已经存在，请重新录入");
                return jsonResultDto;
            }
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("删除失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 校验数据条目信息，如果数据条目编码以及数据条目类型都相同，则返回true，否则返回false
     *
     * @param dataEntryStr
     * @throws Exception
     */
    @RequestMapping(value = "/importDataEntry")
    @ResponseBody
    public JsonResultDto importDataEntry(String dataEntryStr, String projectId) throws Exception {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            systemDataEntryService.importDataEntry(dataEntryStr, EnumImportDataEntryType.DATABASE.getCode(), projectId);
            jsonResultDto.setSuccess(true);
            jsonResultDto.setMsg("导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("导入失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 校验数据条目信息，如果数据条目编码以及数据条目类型都相同，则返回true，否则返回false
     *
     * @param projectId
     */
    @RequestMapping(value = "/importAllDataEntry")
    @ResponseBody
    public JsonResultDto importAllDataEntry(String projectId) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            systemDataEntryService.importAllDataEntry(EnumImportDataEntryType.DATABASE.getCode(), projectId);
            jsonResultDto.setSuccess(true);
            jsonResultDto.setMsg("导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("导入失败：" + e.getMessage());
        }
        return jsonResultDto;
    }
}
